package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.ArchiveBean;
import com.cz.nongyetongv5.custom.dialog.CustomMyDialog;
import com.cz.nongyetongv5.fragment.ArchiveBaseFragment;
import com.cz.nongyetongv5.fragment.ArchiveChargeFragment;
import com.ronri.viewpagerindicator.TitlePageIndicator;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class ArchiveDetailActivity extends FragmentActivity implements View.OnClickListener {

    private RelativeLayout btnBack;
    private TabAdapter adapter;
    private ViewPager viewPager;
    private TextView tv_right;
    private List<Fragment> mFragments;
    private TitlePageIndicator tpiIndicator;
    private LinearLayout ll_bottom;
    private List<String> tabTitles;
    private ArchiveBean archiveBean;

    private String add_user;
    private String region_code;

    private TextView tv_pass;
    private TextView tv_refuse;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archive_detail);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        archiveBean = (ArchiveBean) getIntent().getSerializableExtra("bean");
        add_user = getIntent().getStringExtra("add_user");
        region_code = getIntent().getStringExtra("region_code");
        tv_pass = findViewById(R.id.tv_pass);
        tv_refuse = findViewById(R.id.tv_refuse);
        tv_pass.setOnClickListener(this);
        tv_refuse.setOnClickListener(this);
        initTablayout();
        adapter = new TabAdapter(getSupportFragmentManager(), mFragments, tabTitles);
        viewPager = findViewById(R.id.vpNewListView);
        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(adapter);
        tpiIndicator = findViewById(R.id.tpiIndicator);
        tpiIndicator.setViewPager(viewPager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.tv_pass:
                CustomMyDialog.Builder builder = new CustomMyDialog.Builder(ArchiveDetailActivity.this);
                builder.setMessage("确认提交？");
                builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        postCheckArchive("3", archiveBean.getId(), "", add_user, region_code);
                    }
                });
                builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.createNormalDialog().show();
                break;
            case R.id.tv_refuse:
                Intent intent = new Intent(ArchiveDetailActivity.this, DialogActivity.class);
                intent.putExtra("add_user", add_user);
                intent.putExtra("region_code", region_code);
                intent.putExtra("ids", archiveBean.getId());
                intent.putExtra("type", "2");
                intent.putExtra("model", "archive");
                startActivityForResult(intent, 9022);
                break;
            default:
                break;
        }
    }

    private void initTablayout() {
        tabTitles = new ArrayList<>();
        mFragments = new ArrayList<>();
        tabTitles.add("基本信息");
        tabTitles.add("负责人信息");
        mFragments.add(ArchiveBaseFragment.newInstance(archiveBean));
        mFragments.add(ArchiveChargeFragment.newInstance(archiveBean));
    }

    /**
     * 适配器代码
     */
    class TabAdapter extends FragmentStatePagerAdapter {
        public List<Fragment> list;
        private List<String> titles;

        public TabAdapter(FragmentManager fm, List<Fragment> list, List<String> titles) {
            super(fm);
            this.list = list;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            //            EntListFragment fragment = EntListFragment.newInstance();
            //            Bundle args = new Bundle();
            //            args.putInt("type", 0);
            //            args.putString("args", szTabTitle[position]);
            //            args.putInt("single",position+1);
            //            fragment.setArguments(args);
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
    void postCheckArchive(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "ent/checkEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ArchiveDetailActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(ArchiveDetailActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(ArchiveDetailActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9022:
                    setResult(Activity.RESULT_OK);
                    finish();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

